/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semac.server.rpc;

import java.sql.SQLException;
import java.util.ArrayList;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.semac.client.rpc.ChamadaRPCService;
import com.semac.server.database.classesbase.Database;
import com.semac.server.database.fetchers.PresencaFetcher;
import com.semac.shared.exceptions.RPCExceptions;
import com.semac.shared.model.Atividade;
import com.semac.shared.model.Presenca;

public class ChamadaRPCServiceImpl extends RemoteServiceServlet implements ChamadaRPCService {
	private static final long serialVersionUID = -8637004533574726400L;

	@Override
	public ArrayList<Presenca> getPessoasInscritas(Atividade atividade) throws RPCExceptions {
		PresencaFetcher fetcher = new PresencaFetcher("\"inscricao\".\"codAtividade\" = " + atividade.getId());
		try {
			Database.getInstance().doSelect(fetcher);
		} catch (SQLException e) {
			throw new RPCExceptions("Erro ao selecionar lista de inscritos");
		}
		
		return fetcher.getFetchedData();
	}

	
	@Override
	public String salvaPresenca(Presenca presenca) {
		
		try {
			Database.getInstance().update("inscricao", 
												  "\"presenca\"=" + presenca.isPresente(), 
												  "\"codAtividade\" = " + presenca.getIdAtividade() + " and " +
												  "\"codPessoa\" = " + presenca.getPessoa().getIdPessoa());
		} catch (SQLException e) {
			return "Erro ao setar presença";
		}
		
		return "Presença alterada para " + presenca.getPessoa().getNome();		
	}	
	
}
